package com.wry.wry_test.demo;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @Author: wangrongyi
 * @Date: 2025/1/8 14:42
 * @Description:
 */
@Slf4j
public class ZeroCopyDemo {
    public static void main(String[] args) throws IOException {

        // 打开文件输入流和输出流
        FileInputStream fileInputStream = new FileInputStream("E:\\input.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\output.txt");

        // 获取文件通道
        FileChannel inputChannel = fileInputStream.getChannel();
        FileChannel outputChannel = fileOutputStream.getChannel();

        // 使用 transferTo() 方法直接传输数据
        long transferred = inputChannel.transferTo(0, inputChannel.size(), outputChannel);

        log.info("Transferred bytes: {}", transferred);

        // 关闭资源
        inputChannel.close();
        outputChannel.close();

    }
}
